package com.wys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wys.dao.SetmealDao;
import com.wys.dto.DishDto;
import com.wys.dto.SetmealDto;
import com.wys.exception.CustomerException;
import com.wys.pojo.*;
import com.wys.result.R;
import com.wys.service.CategoryService;
import com.wys.service.SetmealDishService;
import com.wys.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealDao,Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService stmealDishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐菜品
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional()
    public R<String> insert(SetmealDto setmealDto) {
        //套餐保存入套餐表
        super.save(setmealDto);
        //套餐菜品保存如SetmealDish表
        List<SetmealDish> setmealDishesList = setmealDto.getSetmealDishes();
        setmealDishesList = setmealDishesList.stream().map((item) ->{

            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        stmealDishService.saveBatch(setmealDishesList);
        return R.success("套餐添加成功！");
    }

    /**
     *  分页查询
     * @param page 当前页
     * @param pageSize 页面大小
     * @param name 查询的套餐名称
     * @return
     */
    @Override
    public Page getPage(int page, int pageSize, String name) {
        Page<Setmeal> setmealPage =new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name != null,Setmeal::getName,name);
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        page(setmealPage, wrapper);
        //在查询的套餐基础上加入套餐分类
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtos = records.stream().map((item) ->{
            SetmealDto setmealDto =new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if(category != null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtos);
        return setmealDtoPage;
    }
    /**
     *  删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithSetmeal(List<Long> ids) {
        //判断套餐的状态
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        //如果套餐是起售状态则不能删除，抛出一个异常
        int count = this.count(wrapper);
        if(count > 0){
            throw new  CustomerException("套餐处于起售状态，不能删除！");
        }
        //若是停售，则删除套餐
        removeByIds(ids);
        //同时删除套餐菜品表中的数据
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        stmealDishService.remove(wrapper1);
    }

    /**
     *  修改套餐状态
     * @param status  要修改的状态
     * @param ids  要修改的id数组
     * @return
     */
    @Override
    public void updateWithStatus(Integer status, List<Long> ids) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        for (Long id : ids) {
            setmeal.setId(id);
            updateById(setmeal);
        }
    }

    /**
     *  根据ID查询套餐信息，回显示数据
     * @param id
     * @return
     */
    @Override
    public R<SetmealDto> getWithSetmealById(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        //根据ID查询setmeal的基本信息
        Setmeal setmeal = getById(id);
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询改套餐的具体菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        Long setmealId = setmeal.getId();
        wrapper.eq(SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> setmealDishList = stmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(setmealDishList);
        return R.success(setmealDto);
    }

    /**
     *  修改套餐信息
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public void updateWithSetmeal(SetmealDto setmealDto) {
        //更新套餐信息
        this.updateById(setmealDto);
        //更新套餐中的菜品表，先删除原有菜品信息，在重新添加菜品
        LambdaQueryWrapper<SetmealDish> wrapper =new LambdaQueryWrapper<>();
        //套餐ID
        Long setmealId = setmealDto.getId();
        wrapper.eq(SetmealDish::getSetmealId,setmealId);
        stmealDishService.remove(wrapper);
        //重新添加
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) ->{
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        stmealDishService.saveBatch(setmealDishes);
    }

    /**
     *  移动端查询套餐
     * @param setmeal
     * @return
     */
    @Override
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId")
    public R<List<SetmealDto>> getSetmealMove(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.like(setmeal.getName() != null,Setmeal::getName,setmeal.getName());
        wrapper.eq(Setmeal::getStatus,1);
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = super.list(wrapper);
        List<SetmealDto> setmealDtoList = setmealList.stream().map((item) ->{
            //先将基本的套餐信息进行拷贝
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            //查询每一个套餐对应的菜品
            Long setmealId = item.getId();
            LambdaQueryWrapper<SetmealDish> setmealWrapper = new LambdaQueryWrapper<>();
            setmealWrapper.eq(SetmealDish::getSetmealId,setmealId);
            List<SetmealDish> setmealDishList = stmealDishService.list(setmealWrapper);
            setmealDto.setSetmealDishes(setmealDishList);
            return setmealDto;
        }).collect(Collectors.toList());
        return R.success(setmealDtoList);
    }
}
